package com.jerei.plugin.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jerei.plugin.dao.IWcmCommonMenuDetailDao;
import com.jerei.plugin.dao.IWcmCommonRoleMenuDetailDao;
import com.jerei.plugin.entity.WcmCommonMenuDetail;
import com.jerei.plugin.entity.WcmCommonRoleMenuDetail;
import com.jerei.plugin.service.IRoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by sunfeihu on 2016/12/7.
 */
@Service
public class RoleMenuServiceImpl implements IRoleMenuService {

    @Autowired
    private IWcmCommonMenuDetailDao iWcmCommonMenuDetailDao;

    @Autowired
    private IWcmCommonRoleMenuDetailDao iWcmCommonRoleMenuDetailDao;

    @Override
    public String treeGroupMenu(Long roleId, Long gid) {
        List<WcmCommonMenuDetail> children = iWcmCommonMenuDetailDao.getGroupRootMenuDetailNoPower(gid);
        JSONArray array = new JSONArray();
        if (null != children && children.size() > 0) {
            //查询角色拥有的菜单ID
            List<WcmCommonMenuDetail> has = iWcmCommonMenuDetailDao.getMenuDetailsByRole(roleId);
            Set<Long> menuIds = new HashSet<Long>();
            if (null != has) {
                for (WcmCommonMenuDetail menuDetail : has) {
                    menuIds.add(menuDetail.getId());
                }
            }
            List<WcmCommonMenuDetail> _children = null;
            JSONObject obj = null;
            JSONArray childrenArr = null;
            for (WcmCommonMenuDetail child : children) {
                obj = new JSONObject();
                obj.put("id", child.getId());
                obj.put("name", child.getName());
                if (menuIds.contains(child.getId())) {
                    obj.put("checked", true);
                    obj.put("cked", true);
                } else {
                    obj.put("checked", false);
                    obj.put("cked", false);
                }
                childrenArr = this.children(child.getId(), menuIds);
                if (null != childrenArr) {
                    obj.put("children", childrenArr);
                }
                array.add(obj);
            }
        }
        return array.toJSONString();
    }

    @Override
    public String treeDevMenu(Long roleId) {
        List<WcmCommonMenuDetail> children = iWcmCommonMenuDetailDao.getDevRootMenuDetailNoPower();
        JSONArray array = new JSONArray();
        if (null != children && children.size() > 0) {
            //查询角色拥有的菜单ID
            List<WcmCommonMenuDetail> has = iWcmCommonMenuDetailDao.getMenuDetailsByRole(roleId);
            Set<Long> menuIds = new HashSet<Long>();
            if (null != has) {
                for (WcmCommonMenuDetail menuDetail : has) {
                    menuIds.add(menuDetail.getId());
                }
            }
            List<WcmCommonMenuDetail> _children = null;
            JSONObject obj = null;
            JSONArray childrenArr = null;
            for (WcmCommonMenuDetail child : children) {
                obj = new JSONObject();
                obj.put("id", child.getId());
                obj.put("name", child.getName());
                if (menuIds.contains(child.getId())) {
                    obj.put("checked", true);
                    obj.put("cked", true);
                } else {
                    obj.put("checked", false);
                    obj.put("cked", false);
                }
                childrenArr = this.children(child.getId(), menuIds);
                if (null != childrenArr) {
                    obj.put("children", childrenArr);
                }
                array.add(obj);
            }
        }
        return array.toJSONString();
    }

    private JSONArray children(Long pid, Set<Long> menuIds) {
        List<WcmCommonMenuDetail> children = null;
        try {
            children = iWcmCommonMenuDetailDao.getMenuDetailsByParentNoPower(pid);
        } catch (EmptyResultDataAccessException e) {
            //如果结果集为空
            children = null;
        }
        JSONArray array = null;
        if (null != children && children.size() > 0) {
            array = new JSONArray();
            List<WcmCommonMenuDetail> _children = null;
            JSONObject obj = null;
            JSONArray childrenArr = null;
            for (WcmCommonMenuDetail child : children) {
                obj = new JSONObject();
                obj.put("id", child.getId());
                obj.put("name", child.getName());
                if (menuIds.contains(child.getId())) {
                    obj.put("checked", true);
                    obj.put("cked", true);
                } else {
                    obj.put("checked", false);
                    obj.put("cked", false);
                }
                childrenArr = this.children(child.getId(), menuIds);
                if (null != childrenArr) {
                    obj.put("children", childrenArr);
                }
                array.add(obj);
            }
        }
        return array;
    }

    @Override
    public void addPowerForRole(Long roleId, Long[] ids) {
        List<Long> idList = new ArrayList<Long>();
        idList.addAll(Arrays.asList(ids));
        List<WcmCommonRoleMenuDetail> list = iWcmCommonRoleMenuDetailDao.find(roleId, ids);
        Set<Long> exisIds = new HashSet<Long>();
        if (null != list) {
            for (WcmCommonRoleMenuDetail wcmCommonRoleMenuDetail : list) {
                //添加已存在的菜单
                exisIds.add(wcmCommonRoleMenuDetail.getMenuDetailId());
                //移除已存在的菜单
                idList.remove(wcmCommonRoleMenuDetail.getMenuDetailId());
            }
        }
        //添加不存在的菜单
        if (null != idList && idList.size() > 0) {
            ids = new Long[idList.size()];
            iWcmCommonMenuDetailDao.addPowerForRole(roleId, idList.toArray(ids));
        }
        //修改已存在的权限
        if (null != exisIds && exisIds.size() > 0) {
            ids = new Long[exisIds.size()];
            iWcmCommonMenuDetailDao.updatePowerForRole(roleId, exisIds.toArray(ids));
        }
    }

    @Override
    public void deletePowerForRole(Long roleId, Long[] ids) {
        iWcmCommonMenuDetailDao.deletePowerForRole(roleId, ids);
    }

    @Override
    public void clearPowerForRole(Long roleId) {
        iWcmCommonMenuDetailDao.clearPowerForRole(roleId);
    }
}
